<?php
$ubigeo = '';
include('../../Connections/KRAPPERCON.php');
if (isset( $_POST['ubigeo'])) {
$ubigeo = $_POST['ubigeo'];
}

//$ubigeo = '150101';

$dep = '';
$prov = '';
$ciu = '';
if (isset($ubigeo) and trim($ubigeo) !=''){
//echo 'UBIGEO:'.$ubigeo;
//echo $ubigeo."/".$ug_dep."/".$ug_prv;

// Cargar el Combo con la direccion enviada
	if ($ubigeo != ''){
	$ug_dep = substr($ubigeo,0,2);
	$ug_prv = substr($ubigeo,0,4);
	$ug_ciu = substr($ubigeo,0,6);
	
	$sqlx = 'select UBIGEOID, UBIGEOTXT,UBIGEOTYPE from tt_ubigeo';
	$resx = $mysqli->query($sqlx);
	$combo_dep = "<select id='cb_depa' onchange='loadProvincias(this)'>";
	$combo_prv = "<select id='cb_prov' onchange='loadCiudades(this)'>";
	$combo_ciu = "<select id='cb_ubigeo'>";
	if ($resx) {
		while( $row = $resx->fetch_assoc() ){
			$sel = '';
			//echo $row['ubigeotype'];
			if ($row['UBIGEOTYPE'] == 1){
				if ($row['UBIGEOID'] == $ug_dep) $sel = " selected='selected' ";
				$combo_dep = $combo_dep."<option ".$sel." value='".$row['UBIGEOID']."'>".$row['UBIGEOTXT']."</option>";
			}
			else if ($row['UBIGEOTYPE'] == 2 ){
				if ($row['UBIGEOID'] == $ug_prv) $sel = " selected='selected' ";
				$combo_prv = $combo_prv."<option ".$sel." value='".$row['UBIGEOID']."'>".$row['UBIGEOTXT']."</option>";
			}
			else if ($row['UBIGEOTYPE'] == 3 ){
				if ($row['UBIGEOID'] == $ug_ciu) $sel = " selected='selected' ";
				$combo_ciu = $combo_ciu."<option ".$sel."value='".$row['UBIGEOID']."'>".$row['UBIGEOTXT']."</option>";
			}
		}
	$resx->close();
			if ($mysqli->more_results()){
			$mysqli->check_result();
			}
		}
	$combo_dep = $combo_dep."</select>";
	$combo_prv = $combo_prv."</select>";
	$combo_ciu = $combo_ciu."</select>";
	}
echo "<span>".$combo_dep."</span>"	;
echo "<span id='sp_provin'>".$combo_prv."</span>";
echo "<span id='sp_ciudad'>".$combo_ciu."</span>";
exit;
}



//------------------------------------------------------------
// Carga Normal de Ubicaciones
//------------------------------------------------------------
if (isset($_POST['depart'])) {
// Muestra Provincias por Departamento
//include('../../Connections/KRAPPERCON.php');
$dep = $_POST['depart'];
$sql = "select UBIGEOID, UBIGEOTXT from tt_ubigeo WHERE UBIGEOTOP = '".$dep."'";
$res = $mysqli->query($sql);
echo "<select id='cb_prov' onchange='loadCiudades(this)'>";
if ($res){
	if ($mysqli->affected_rows == 1){
		echo "<option value='".$row['UBIGEOID']."'>[Seleccione]</option>";		
	}
while( $row = $res->fetch_assoc()) {
echo "<option value='".$row['UBIGEOID']."'>".$row['UBIGEOTXT']."</option>";
}
$res->close();
			
			$mysqli->check_result();
			

}
echo "</select>";
exit;
}

if (isset($_POST['provin'])) {
// Muestra Ciudades por provincia
//include('../../Connections/KRAPPERCON.php');
$prv = $_POST['provin'];
$sql = "select UBIGEOID, UBIGEOTXT from tt_ubigeo WHERE UBIGEOTOP = '".$prv."'";
$res = $mysqli->query($sql);
echo "<select id='cb_ubigeo' >";
if ($res){
while( $row = $res->fetch_assoc()) {
echo "<option value='".$row['UBIGEOID']."'>".$row['UBIGEOTXT']."</option>";
}
$res->close();
$mysqli->check_result();

}
echo "</select>";
exit;
}

?>
<span>
<?php 
// Lista todos los Departamentos
$sql = 'select UBIGEOID, UBIGEOTXT from tt_ubigeo WHERE UBIGEOTYPE = 1';
$res = $mysqli->query($sql);
echo "<select id='cb_depa' onchange='loadProvincias(this)'>";
if ($res){
while( $row = $res->fetch_assoc()) {
echo "<option value='".$row['UBIGEOID']."'>".$row['UBIGEOTXT']."</option>";
}
$res->close();
$mysqli->check_result();
}
echo "</select>";
//mysql_close($db);
?>
</span>
<span id="sp_provin"></span>
<span id="sp_ciudad"></span>